package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.qo.HealthDataQo;
import com.ruoyi.system.domain.vo.HealthDataVo;
import com.ruoyi.system.domain.vo.HeartRateIndexVo;
import com.ruoyi.system.service.ISysAlertService;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysHealthDataMapper;
import com.ruoyi.system.domain.SysHealthData;
import com.ruoyi.system.service.ISysHealthDataService;

/**
 * 健康数据Service业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysHealthDataServiceImpl implements ISysHealthDataService {
    private final SysHealthDataMapper sysHealthDataMapper;

    private final ISysAlertService sysAlertService;

    public SysHealthDataServiceImpl(ISysAlertService sysAlertService, SysHealthDataMapper sysHealthDataMapper) {
        this.sysAlertService = sysAlertService;
        this.sysHealthDataMapper = sysHealthDataMapper;
    }

    /**
     * 查询健康数据信息
     *
     * @param id 健康数据ID
     * @return 健康数据信息
     */
    @Override
    public SysHealthData selectSysHealthDataById(Long id) {
        return sysHealthDataMapper.selectSysHealthDataById(id);
    }

    /**
     * 查询健康数据列表
     *
     * @param sysHealthData 健康数据信息
     * @return 健康数据
     */
    @Override
    public List<SysHealthData> selectSysHealthDataList(SysHealthData sysHealthData) {
        return sysHealthDataMapper.selectSysHealthDataList(sysHealthData);
    }

    @Override
    public List<HealthDataVo> selectHealthDataList(HealthDataQo healthDataQo) {
        // 如果未设置开始时间，则默认为当前时间往前推5分钟
        if (healthDataQo.getStartTime() == null) {
            long fiveMinutesAgo = System.currentTimeMillis() - 30 * 60 * 1000;
            healthDataQo.setStartTime(new Date(fiveMinutesAgo));
        }
        return sysHealthDataMapper.selectHealthDataList(healthDataQo);
    }

    @Override
    public HeartRateIndexVo getHeartRateIndex(HealthDataQo healthDataQo) {
        // 如果未设置开始时间，则默认为当前时间往前推5分钟
        if (healthDataQo.getStartTime() == null) {
            long fiveMinutesAgo = System.currentTimeMillis() - 5 * 60 * 1000;
            healthDataQo.setStartTime(new Date(fiveMinutesAgo));
        }
        return sysHealthDataMapper.getHeartRateIndex(healthDataQo);
    }

    /**
     * 根据设备ID查询所有特征类型的最新健康数据
     *
     * @param deviceId 设备ID
     * @return 健康数据集合
     */
    @Override
    public List<SysHealthData> selectLatestByDeviceId(String deviceId) {
        return sysHealthDataMapper.selectLatestByDeviceId(deviceId);
    }

    /**
     * 根据用户ID查询所有特征类型的最新健康数据
     *
     * @param userId 用户ID
     * @return 健康数据集合
     */
    @Override
    public List<SysHealthData> selectLatestByUserId(Long userId) {
        return sysHealthDataMapper.selectLatestByUserId(userId);
    }

    /**
     * 新增健康数据
     *
     * @param sysHealthData 健康数据信息
     * @return 结果
     */
    @Override
    public int insertSysHealthData(SysHealthData sysHealthData) {
        return sysHealthDataMapper.insertSysHealthData(sysHealthData);
    }

    /**
     * 批量新增健康数据
     *
     * @param sysHealthDataList 健康数据列表
     * @return 结果
     */
    @Override
    public int batchInsertSysHealthData(List<SysHealthData> sysHealthDataList) {
        return sysHealthDataMapper.batchInsertSysHealthData(sysHealthDataList);
    }

    /**
     * 修改健康数据
     *
     * @param sysHealthData 健康数据信息
     * @return 结果
     */
    @Override
    public int updateSysHealthData(SysHealthData sysHealthData) {
        return sysHealthDataMapper.updateSysHealthData(sysHealthData);
    }

    /**
     * 批量删除健康数据
     *
     * @param ids 需要删除的健康数据ID
     * @return 结果
     */
    @Override
    public int deleteSysHealthDataByIds(Long[] ids) {
        return sysHealthDataMapper.deleteSysHealthDataByIds(ids);
    }

    /**
     * 删除健康数据信息
     *
     * @param id 健康数据ID
     * @return 结果
     */
    @Override
    public int deleteSysHealthDataById(Long id) {
        return sysHealthDataMapper.deleteSysHealthDataById(id);
    }

    /**
     * 生成模拟的健康数据并保存到数据库，然后调用generateAlertsFromHealthData生成报警数据
     *
     * @param heartRate     心率数据  90 单位:次/分钟
     * @param bloodPressure 血压数据  95,125 收缩压,舒张压, 单位:mmHg
     * @param bloodOxygen   血氧数据  95 单位:%
     */
    @Override
    public void generateMockHealthData(String heartRate, String bloodPressure, String bloodOxygen, Long userId, Long deviceId) {
        // 模拟设备ID和用户ID
        if (userId == null) {
            userId = 1L;
        }
        if (deviceId == null) {
            deviceId = 1L;
        }

        // 模拟健康数据
        SysHealthData healthData = new SysHealthData();
        healthData.setDeviceId(deviceId);
        healthData.setUserId(userId);
        healthData.setHeartRate(heartRate);
        healthData.setBloodPressure(bloodPressure);
        // 模拟心率数据
        healthData.setBloodOxygen(bloodOxygen);
        healthData.setUpdateTime(new Date());
        healthData.setCreateTime(new Date());

        // 保存模拟的健康数据
        sysHealthDataMapper.insertSysHealthData(healthData);

        // 调用generateAlertsFromHealthData生成报警数据
        sysAlertService.generateAlertsFromHealthData(healthData);
    }
}